﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using RIDA.DiagramDesigner;
using RIDA.Globals;
using RIDA.DiagramDesigner.ViewModel;

namespace RIDA.DiagramDesigner
{
    public class Connection : Control, ISelectable
    {
        private Adorner connectionAdorner;

        #region Dependencies Properties
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(Connection), new UIPropertyMetadata(false,new PropertyChangedCallback(OnIsSelectedChanged)));

        #endregion

        public Connection()
        {
            base.Unloaded += new RoutedEventHandler(Connection_Unloaded);
            base.LayoutUpdated += new EventHandler(Connection_LayoutUpdated);
        }

        void Connection_LayoutUpdated(object sender, EventArgs e)
        {
            Rect itemRect = VisualTreeHelper.GetDescendantBounds(this);
            var viewModel = this.DataContext as ConnectionViewModel;
            viewModel.BoundingRect = itemRect;
        }

        public static void OnIsSelectedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            Connection con=o as Connection;
            if ((bool)e.NewValue)
                con.ShowAdorner();
            else
                con.HideAdorner();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            var vm = this.DataContext as ConnectionViewModel;
            DesignerCanvas designer = this.GetParent<DesignerCanvas>();
            if (designer != null)
                if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
                    if (vm.Selected)
                    {
                        vm.Selected = false;
                        designer.SelectedItems.Remove(vm);
                    }
                    else
                    {
                        vm.Selected = true;
                        designer.SelectedItems.Add(vm);
                    }
                else if (!vm.Selected)
                {
                    foreach (DiagramElementViewModelBase item in designer.SelectedItems)
                        item.Selected = false;

                    designer.SelectedItems.Clear();
                    vm.Selected = true;
                    designer.SelectedItems.Add(vm);
                }
            e.Handled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        private void ShowAdorner()
        {
            if (this.connectionAdorner == null)
            {
                DesignerCanvas designer = this.GetParent<DesignerCanvas>();

                AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(designer);
                if (adornerLayer != null)
                {
                    this.connectionAdorner = new ConnectionAdorner(designer, this);
                    adornerLayer.Add(this.connectionAdorner);
                }
            }
            this.connectionAdorner.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 
        /// </summary>
        private void HideAdorner()
        {
            if (this.connectionAdorner != null)
                this.connectionAdorner.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Connection_Unloaded(object sender, RoutedEventArgs e)
        {
            if (this.connectionAdorner != null)
            {
                DesignerCanvas designer = this.GetParent<DesignerCanvas>();

                AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(designer);
                if (adornerLayer != null)
                {
                    adornerLayer.Remove(this.connectionAdorner);
                    this.connectionAdorner = null;
                }
            }
        }
    }

    public enum ArrowSymbol
    {
        None,
        Arrow,
        Diamond
    }
}
